//
//  DiscoverViewController.m
//  News
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DataHelper.h"
#import "VideoCell.h"
#import "MJRefresh.h"
#import "VideoModel.h"
#import "VideoController.h"
#import "MBProgressHUD+MJ.h"
#import "DownloadListController.h"
#import "AFNetworkReachabilityManager.h"
#import "MovieViewController.h"
#import "dataBaseHelper.h"
#import "ZZVideoList.h"

#define kDragDownUrls @"http://c.m.163.com/nc/video/list/V9LG4B3A0/y/0-10.html"
#define kDragUpUrls @"http://c.m.163.com/nc/video/list/V9LG4B3A0/y/%d-%d.html"

@interface DiscoverViewController () <UITableViewDataSource, UITableViewDelegate, VideoCellDelegate>

@property (nonatomic, strong) UITableView *videoTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) int dragDownCount;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"发现";
    [self.navigationController.navigationBar setBackgroundColor:[UIColor orangeColor]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xiazai"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpToDownLoadList)];
    
    self.videoTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    [MBProgressHUD showMessage:@"加载中...."];
    [[DataHelper sharedDataHelper] requestDataFromNetWorkWithUrl:kDragDownUrls didFinished:^{
        self.dataArray = [DataHelper sharedDataHelper].dataArray;
       
        [MBProgressHUD hideHUD];
        [self.videoTableView reloadData];
    }];
    
    
    self.videoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    // 下拉刷新
    _videoTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[DataHelper sharedDataHelper] requestDataFromNetWorkWithUrl:kDragDownUrls didFinished:^{
            self.dataArray = [DataHelper sharedDataHelper].dataArray;
            [self.videoTableView reloadData];
        }];
        // 结束刷新
        [_videoTableView.header endRefreshing];
        
    }];
    
    
    // 上拉刷新
    _videoTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _dragDownCount++;
        [[DataHelper sharedDataHelper] requestNewDataFromNetWorkWithUrl:[NSString stringWithFormat:kDragUpUrls                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              , 10 * _dragDownCount, 10] didFinished:^{
            self.dataArray = [DataHelper sharedDataHelper].dataArray;
           
            [self.videoTableView reloadData];
            [_videoTableView.footer endRefreshing];
        }];
        
    }];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCell *cell = [VideoCell cellWithTableView:tableView];
    
    VideoModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    VideoController *videoVC = [[VideoController alloc] init];
//    VideoModel *model = self.dataArray[indexPath.row];
//    videoVC.videoModel = model;
//    [self.navigationController pushViewController:videoVC animated:YES];
    
    
    VideoModel *model = self.dataArray[indexPath.row];
    ZZVideoList *videoModel = [[ZZVideoList alloc] init];
    videoModel.duration = [model.length_video doubleValue];
    videoModel.title = model.title;
    
    MovieViewController *mo = [[MovieViewController alloc]init];
    mo.UrlString = model.mp4_url;
    mo.receiveModel = videoModel;
    [self showDetailViewController:mo sender:nil];
    [[dataBaseHelper shareInstance]addCollectiObject:videoModel];
}

#pragma mark - VideoCellDelegate
- (void)videoCell:(VideoCell *)videoCell ShowAlertController:(UIAlertController *)alert
{
    [self presentViewController:alert animated:YES completion:nil];
    NSLog(@"%@", alert.title);
}

#pragma mark - private Method
- (void)jumpToDownLoadList
{
    [self.navigationController pushViewController:[DownloadListController sharedDownloadListController] animated:YES];
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - 懒加载
- (UITableView *)videoTableView
{
    if (_videoTableView == nil) {
        _videoTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.videoTableView.delegate = self;
        self.videoTableView.dataSource = self;
        [self.view addSubview:_videoTableView];
    }
    return _videoTableView;
}


@end
